package com.feng.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * TTL队列 配置文件
 */
@Configuration
public class TtlQueueConfig {
    //普通交换机
    public static final String X_EXCHANGE = "X";
    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE = "Y";
    //普通队列
    public static final String QA_QUEUE = "QA";
    public static final String QB_QUEUE = "QB";
    //死信队列
    public static final String DEAD_LETTER_QUEUE = "QD";
    //普通队列：延迟时间不定
    public static final String QC_QUEUE = "QC";

    /**
     * 声明普通队列C
     * @return
     */
    @Bean("queue_c")
    public Queue queueC(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        return QueueBuilder.durable(QC_QUEUE).withArguments(arguments).build();
    }
    /**
     * 队列C和交换机绑定
     */
    @Bean
    public Binding queueCindingX(@Qualifier("queue_c") Queue queueC,
                                 @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

    /**
     * 声明交换机
     */
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 声明普通队列
     * TTL 为10s、40s
     * @return
     */
    @Bean("queue_a")
    public Queue queueA(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",1000*10);
        return QueueBuilder.durable(QA_QUEUE).withArguments(arguments).build();
    }
    @Bean("queue_b")
    public Queue queueB(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","YD");
        arguments.put("x-message-ttl",1000*40);
        return QueueBuilder.durable(QB_QUEUE).withArguments(arguments).build();
    }
    /**
     * 死信队列
     */
    @Bean("queue_d")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    /**
     * 队列和交换机绑定
     */
    @Bean
    public Binding queueAindingX(@Qualifier("queue_a") Queue queueA,
                                 @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }
    @Bean
    public Binding queueBindingX(@Qualifier("queue_b") Queue queueB,
                                 @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    @Bean
    public Binding queueDindingY(@Qualifier("queue_d") Queue queueD,
                                 @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
}
